package cn.jiyun.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.stream.CollectorUtil;
import cn.jiyun.feign.*;
import cn.jiyun.pojoday07.Labels;
import cn.jiyun.pojoday08.Course;
import cn.jiyun.pojoday08.CourseDTO;
import cn.jiyun.pojoday08.CourseVO;
import cn.jiyun.pojoday09.MenusEmp;
import cn.jiyun.pojoday09.MenusResult;
import cn.jiyun.pojoday10.TopicEmp;
import cn.jiyun.pojoday10.TopicVO;
import cn.jiyun.pojoday11.PaperDTO;
import cn.jiyun.pojoday11.PaperTopic;
import cn.jiyun.pojoday11.PaperVO;
import cn.jiyun.pojoday12.*;
import cn.jiyun.pojoday14.Emp;
import cn.jiyun.pojoday15.Train;
import cn.jiyun.pojoday15.TrainDTO;
import cn.jiyun.pojoday15.TrainVO;
import cn.jiyun.utils.Result;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.utils.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.prometheus.client.Collector;
import net.sf.jsqlparser.statement.select.Top;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 小周
 * @Package: cn.jiyun.controller
 * @Project: 2210A-project1
 * @name: ShouController
 * @Date: 2023/8/23 19:34
 * @Filename: ShouController
 */
@RestController
@RequestMapping("/shou")
public class ShouController {

    @Autowired
    private TestService testService;
    @Autowired
    private TestService1 testService1;
    @Autowired
    private TestService2 testService2;
    @Autowired
    private TestService3 testService3;
    @Autowired
    private TestService4 testService4;
    @Autowired
    private TestService5 testService5;
    @Autowired
    private TestService6 testService6;
    @Autowired
    private RedisTemplate redisTemplate;

    //课程模块
    @GetMapping("findZan")
    public Result findZan(Integer id,HttpServletRequest request){
        //先根据课程id  去查询是否点过赞
        String s = (String) redisTemplate.opsForHash().get("课程点赞", id.toString());
        if (!StringUtils.isBlank(s)){
            //获取当前登录人
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s1 = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s1, Emp.class);
            //如果查询到   判断点赞的eid中有没有当前登录人的id  如果
            if (s.contains("eid"+emp.getEid())){
                //有 就是已经点过赞  进行取消点赞操作  删除这个eid的记录 字符串替换成空串
                String s2 = s.replace("eid" + emp.getEid(), "");
                //存到redis中
                redisTemplate.opsForHash().put("课程点赞",id.toString(),s2);
                return Result.ok().message("取消点赞成功");
            }else{
                //该用户没有点过赞  进行点赞操作 将当前登录人的eid拼接到点赞的字符串中
                String s2 = s + "eid" + emp.getEid();
                //存到redis中
                redisTemplate.opsForHash().put("课程点赞",id.toString(),s2);
                return Result.ok().message("点赞成功");
            }
        }else{
            //给一个默认值
            redisTemplate.opsForHash().put("课程点赞",id.toString(),"1");
        }
        return Result.ok();
    }
    @RequestMapping("toCourseAll")
    public Result toCourseAll(@RequestBody CourseDTO courseDTO){
        //先根据条件去查询课程
        PageInfo<CourseVO> list= testService2.toCourseAll(courseDTO);
        //遍历集合  根据课程id 去查询点赞数量
        for (CourseVO course : list.getList()) {
            //根据课程id去redis中查询点赞数量
            String s = (String) redisTemplate.opsForHash().get("课程点赞", course.getId().toString());
            //如果能查询到
            if (!StringUtils.isBlank(s)){
                //判断该保存的格式是否正确
                if (s.contains("eid")){
                    //正确的话 切割 成数组 数组长度减去一个默认值就是该课程的点赞数量
                    String[] arr = s.split("eid");
                    int i=arr.length;
                    course.setZan(i-1);
                }else{
                    course.setZan(0);
                }
            }
        }
        //判断是否是点击最热按钮发起的查询请求
        if (null!=courseDTO.getSou() && courseDTO.getSou()==3){
            //如果是的话  根据点赞数量进行倒叙排序后 返回
            Collections.sort(list.getList(),(a,b)->b.getZan().compareTo(a.getZan()));
        }
        return Result.ok().data("PageInfo",list);
    }
    @RequestMapping("xueMenus")
    public Result xueMenus(@RequestBody MenusEmp menusEmp){
        try {
            int i=testService5.xueMenus(menusEmp);
            if (i==1){
                return Result.ok().message("学习完成！！");
            }else{
                return Result.error().message("学习失败！！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }


   //竞赛模块
   @GetMapping("findPaperByCid")
   public Result findPaperByCid(String cid){
       try {
           List<CompetPaperVO> list =testService.findPaperByCid(cid);
           return Result.ok().data("pList",list);
       }catch (Exception e){
           e.printStackTrace();
           return Result.error();
       }
   }
   //培训模块
   @RequestMapping("toTrainAll")
   public Result toTrainAll(@RequestBody TrainDTO trainDTO){
       try {
           PageInfo<TrainVO> pageInfo=testService1.toTrainAll(trainDTO);
           return Result.ok().data("PageInfo",pageInfo);
       }catch (Exception e){
           e.printStackTrace();
           return Result.error();
       }
   }

















    @GetMapping("findMyCompet")
    public Result findMyCompet(Integer eid){
        try {
           List<CompetVO> list=testService.findMyCompet(eid+"");
            return Result.ok().data("MyCompetList",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("findMyTrain")
    public Result findMyTrain(Integer eid){
        try {
            List<TrainVO> list=testService1.findMyTrain(eid+"");
            return Result.ok().data("MyTrainList",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("findMyCourse")
    public Result findMyCourse(Integer eid){
        try {
            List<CourseVO> list=testService1.findMyCourse(eid+"");
            for (CourseVO vo : list) {
                    //去查询总目录数
                    String zongshu = testService5.findMenusCounts(vo.getId() + "");
                    //查询学习数量
                    List<MenusEmp> xue = testService5.findCourseXue(vo.getId() + "", eid+ "");
                    Integer size = xue.size();
                    vo.setXuexi(size);
                    vo.setZongshu(Integer.parseInt(zongshu));
            }
            return Result.ok().data("MyCourseList",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }





    @RequestMapping("toPaperAll")
    public Result toPaperAll(@RequestBody PaperDTO paperDTO){
        try {
            PageInfo<PaperVO> list= testService3.toPaperAll(paperDTO);
            return Result.ok().data("PageInfo",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }



    @GetMapping("findCourseByTid")
    public Result findCourseByTid(String tid,HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            //根据用户id  远程调用  去老项目里查询我的课程个数
            List<Course> list =testService1.findCourseByTid(tid);
            List<CourseVO> list1=new ArrayList<>();
            for (Course vo : list) {
                CourseVO courseVO = new CourseVO();
                //去查询总目录数
                String zongshu = testService5.findMenusCounts(vo.getId() + "");
                //查询学习数量
                List<MenusEmp> xue = testService5.findCourseXue(vo.getId() + "", emp.getEid() + "");
                Integer size = xue.size();
                BeanUtil.copyProperties(vo,courseVO);
                courseVO.setXuexi(size);
                courseVO.setZongshu(Integer.parseInt(zongshu));
                //根据id去redis中查询点赞数量
                String s1 = (String) redisTemplate.opsForHash().get("课程点赞", vo.getId().toString());
                if (!StringUtils.isBlank(s1)){
                    if (s1.contains("eid")){
                        String[] arr = s1.split("eid");
                        int i=arr.length;
                        courseVO.setZan(i-1);
                    }else{
                        courseVO.setZan(0);
                    }
                }
                list1.add(courseVO);
            }
            return Result.ok().data("cList",list1);
        }catch (Exception e){
         e.printStackTrace();
         return Result.error();
        }
    }
    @GetMapping("findPaperByTid")
    public Result findPaperByTid(String tid){
        try {
            List<PaperVO> list =testService1.findPaperByTid(tid);
            return Result.ok().data("pList",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findAwners")
    public Result findAwners(@RequestBody TopicEmp topicEmp){
        try {
            TopicEmp AnwersList=testService6.findAwners(topicEmp);
            return Result.ok().data("AnwersList",AnwersList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }



    @RequestMapping("addZwners")
    public Result addZwners(@RequestBody TopicEmp topicEmp){
        try {
            int i=testService6.addZwners(topicEmp);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }


    @GetMapping("toPaperInfo")
    public Result toPaperInfo(String pid){
        try {
            //根据pid去 查询该试卷下的题目集合
            List<PaperTopic> paperTopics=testService3.toPaperInfo(pid);
            //遍历集合  拿到试题的id  去查询试题的详情信息
            List<TopicVO> list=new ArrayList<>();
            Integer i=0;
            for (PaperTopic paperTopic : paperTopics) {
                TopicVO topicVO=testService6.toTopInfo(paperTopic.getTid()+"");
                topicVO.setXiabiao(1+i);
                list.add(topicVO);
                i++;
            }
            return Result.ok().data("PaperInfo",list);
        }catch (Exception e){
         e.printStackTrace();
         return Result.error();
        }
    }






    @RequestMapping("toComAll")
    public Result toComAll(@RequestBody CompetDTO competDTO) {
        //先去查询课程
        PageInfo<CompetVO> list = testService.toComAll(competDTO);
        return Result.ok().data("PageInfo",list);
    }

    @RequestMapping("saveCompet")
    public Result saveCompet(@RequestBody CompetEmp1 competEmp1){
        try {
            CompetVO competVO=testService.saveCompet(competEmp1);
            return Result.ok().data("competInfo",competVO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("toCourseInfo")
    public Result toCourseInfo(Integer cid){
        try {
            CourseVO courseVO=testService2.toCourseInfo(cid);
            //查询点赞数
            //根据id去redis中查询点赞数量
            String s = (String) redisTemplate.opsForHash().get("课程点赞", courseVO.getId().toString());
            if (!StringUtils.isBlank(s)) {
                if (s.contains("eid")) {
                    String[] arr = s.split("eid");
                    int i = arr.length;
                    courseVO.setZan(i - 1);
                } else {
                    courseVO.setZan(0);
                }
            }
            return Result.ok().data("courseInfo",courseVO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("findMenusCounts")
    public Result findMenusCounts(String cid){
        try {
            String courseCounts=testService5.findMenusCounts(cid);
            return Result.ok().data("courseCounts",courseCounts);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("findCourseXue")
    public Result findCourseXue(String cid,String eid){
        try {
            List<MenusEmp> list=testService5.findCourseXue(cid,eid);
            Integer size = list.size();
            return Result.ok().data("courseXue",size).data("MEList",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("findMenusAll")
    public Result findMenusAll(String cid){
        try {
            List<MenusResult> list=testService5.findMenusAll(cid);
            return Result.ok().data("mlist",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("toTrain")
    public Result toTrain(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            //根据用户id  远程调用  去老项目里查询我的课程个数
            List<TrainVO> trainVOS = testService1.toTrain(emp.getEid() + "");
            return Result.ok().data("tList",trainVOS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("toPaper")
    public Result toPaper(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            //根据用户id  远程调用  去老项目里查询我的课程个数
            List<PaperVO> paper=testService3.toPaper(emp.getEid()+"");
            return Result.ok().data("pList",paper);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }



    @RequestMapping("toZan")
    public Result toZan(){
        //先去查询课程
        List<Course> list= testService2.toZan();
        //遍历集合  根据课程id 去查询点赞数量
        List<CourseVO> list2=new ArrayList<>();
        for (Course course : list) {
            //根据id去redis中查询点赞数量
            String s = (String) redisTemplate.opsForHash().get("课程点赞", course.getId().toString());
//                String  s = (String) redisTemplate.opsForValue().get(course.getId().toString());
            CourseVO courseVO = new CourseVO();
            BeanUtil.copyProperties(course, courseVO);
            if (!StringUtils.isBlank(s)){
                if (s.contains("eid")){
                    String[] arr = s.split("eid");
                    int i=arr.length;
                    courseVO.setZan(i-1);
                }else{
                    courseVO.setZan(0);
                }
            }
            list2.add(courseVO);
        }
        return Result.ok().data("courseList",list2);
    }

    @RequestMapping("findNewcom")
    public Result findNewcom(){
        //先去查询课程
        List<Course> list= testService2.findNewcom();
        //遍历集合  根据课程id 去查询点赞数量
        List<CourseVO> list2=new ArrayList<>();
        for (Course course : list) {
            //根据id去redis中查询点赞数量
            String s = (String) redisTemplate.opsForHash().get("课程点赞", course.getId().toString());
//                String  s = (String) redisTemplate.opsForValue().get(course.getId().toString());
            CourseVO courseVO = new CourseVO();
            BeanUtil.copyProperties(course, courseVO);
            if (!StringUtils.isBlank(s)){
                if (s.contains("eid")){
                    String[] arr = s.split("eid");
                    int i=arr.length;
                    courseVO.setZan(i-1);
                }else{
                    courseVO.setZan(0);
                }
            }
            list2.add(courseVO);
        }
        return Result.ok().data("courseList2",list2);
    }
    @RequestMapping("findcom")
    public Result findcom(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            //根据用户id  远程调用  去老项目里查询我的课程个数
            String comNUm=testService.findCom(emp.getEid()+"");
            return Result.ok().data("comNUm",comNUm);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findLabelAll")
    public Result findLabelAll(){
        try {
            List<Labels> list =testService4.findLabelAll();
            return Result.ok().data("Llist",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findemp")
    public Result findemp(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            return Result.ok().data("Emp",emp);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findtNum")
    public Result findtNum(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //根据token去redis中查询用户信息
            String s = (String) redisTemplate.opsForValue().get(token);
            Emp emp = JSONObject.parseObject(s, Emp.class);
            //根据用户id  远程调用  去老项目里查询我的课程个数
            String tNum=testService1.findtNum(emp.getEid()+"");
            return Result.ok().data("tNum",tNum);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

}
